package org.lanqiao.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.lanqiao.collect.Collect;
import org.lanqiao.collect.ICollectService;
import org.lanqiao.information.IInfoService;
import org.lanqiao.information.Information;
import org.lanqiao.mapper.CollectMapper;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

@Service
public class CollectServiceImpl implements ICollectService {
    @Autowired
    private CollectMapper collectMapper;
    @Autowired
    private IInfoService iInfoService;


    @Override
    //无条件查询所有
    public List<Collect> CollectList() {
        return collectMapper.selectList(null);
    }
    @Override
    //新增一个收藏帖子
    public Integer AddCollect(Integer cardid, Integer userid) {
        if (IsExist(cardid, userid)){//判断该用户是否已经收藏
            return 0;
        }else{
            Date createtime =new Date();//当前时间为创建时间
            Collect collect =new Collect(cardid,userid,createtime);
            if(userid!=iInfoService.selectOnes(cardid).getUserid()){//如果当前userid不是该资源的发布者
                //同时在对应的资源的收藏总数加一
                iInfoService.addoneCollectNum(cardid);
                //不允许自己收藏自己的帖子
                return collectMapper.insert(collect);
            }else{
                return 0;
            }
        }
    }
    //判断改用户是否对当前帖子收藏
    private boolean IsExist(Integer cardid, Integer userid){
       Collect collect= collectMapper.selectOne(new QueryWrapper<Collect>()
            .eq("card_id",cardid)
                .eq("user_id",userid)
        );
       if(collect!=null){
           return  true;
       }
       else {
           return  false;
       }
    }

    @Override
    public List<Collect> SelectByCardid(Integer cardid) {
        return collectMapper.selectList(new QueryWrapper<Collect>()
                .eq("card_id",cardid));
    }

    @Override
    //根据userid查询collect
    public IPage<Collect> SelectByUserid(Integer userid, Integer pagenum, Integer pagesize) {
        IPage<Collect> page = new Page<>(pagenum,pagesize);
        page = collectMapper.selectPage(page,new QueryWrapper<Collect>().eq("user_id",userid));
        return page;
    }

    @Override
    //删除收藏贴
    public Integer DeleteCollect(Integer userid,Integer cardid) {
        if(userid!=iInfoService.selectOnes(cardid).getUserid()){//如果当前userid不是该资源的发布者
            //同时在对应的资源的收藏总数减一
            iInfoService.DeloneCollectNum(cardid);
        }
        return collectMapper.delete(new QueryWrapper<Collect>()
            .eq("user_id",userid)
                .eq("card_id",cardid)
        );
    }
    @Override
    //根据cardID删除所有属于该cardID的收藏贴
    public Integer deletelist(Integer cardid){
       return collectMapper.delete(new QueryWrapper<Collect>().eq("card_id",cardid));
    }

}
